## Replication files for "Do Online Ads Influence Vote Choice?"
## anselm.rink@gmail.com
## This code replicates Table 3

rm(list = ls())
options(scipen=999)
set.seed(492960) #taken from random.org
library(foreign)
library(stargazer)
library(ri2)       
setwd("") ## set WD
data <- read.csv("data.csv") 

## define robust SE function 
cluster <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm), 2 , function(x) tapply(x, cluster, sum, na.rm=T));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) 
}


## run OLS models
model1 <- cluster(data, lm(cdu16erst ~ treatment_rational + treatment_emotional, data=data), data$PLZ)
model2 <- cluster(data, lm(cdu16zweit ~ treatment_rational + treatment_emotional, data=data), data$PLZ)
model3 <- cluster(data, lm(cdu16erst ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), data$PLZ)
model4 <- cluster(data, lm(cdu16zweit ~ treatment_rational + treatment_emotional + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11zweit, data=data), data$PLZ)


## print models
stargazer(model1, model2, model3, model4)




#### obtain RI p-values

## background declarations
N <- 100
declaration <- declare_ra(N = 189, m = 126)

## Model 1 Factual
Z <- data$treatment_rational
X <- data$treatment_emotional
Y <- data$cdu16erst
dat <- data.frame(Y, Z)
ri_model1_factual <- conduct_ri(formula = Y ~ Z + X, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model1_factual)[3]


## Model 1 Emotional
Z <- data$treatment_emotional
X <- data$treatment_rational
Y <- data$cdu16erst
dat <- data.frame(Y, Z)
ri_model1_emotional <- conduct_ri(formula = Y ~ Z + X, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model1_emotional)[3]



## Model 2 Factual
Z <- data$treatment_rational
X <- data$treatment_emotional
Y <- data$cdu16zweit
dat <- data.frame(Y, Z)
ri_model2_factual <- conduct_ri(formula = Y ~ Z + X, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model2_factual)[3]


## Model 2 Emotional
Z <- data$treatment_emotional
X <- data$treatment_rational
Y <- data$cdu16zweit
dat <- data.frame(Y, Z)
ri_model2_emotional <- conduct_ri(formula = Y ~ Z + X, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model2_emotional)[3]








## Model 3 Factual
Y <- data$cdu16erst
Z <- data$treatment_rational
X0 <- data$treatment_emotional
X1 <- data$age6_15 
X2 <- data$age15_18 
X3 <- data$age18_27 
X4 <- data$age27_45 
X5 <- data$age45_55 
X6 <- data$age55_65 
X7 <- data$age_o65 
X8 <- data$female 
X9 <- data$protestant 
X10 <- data$catholic 
X11 <- data$atheist_pop 
X12 <- data$foreign_pop 
X13 <- data$total_pop_ln 
X14 <- data$cdu11erst
dat <- data.frame(Y, Z, X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14)
ri_model3_factual <- conduct_ri(formula = Y ~ Z + X0, X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + X13 + X14, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model3_factual)[3]



## Model 3 Emotional
Y <- data$cdu16erst
Z <- data$treatment_emotional
X0 <- data$treatment_rational
X1 <- data$age6_15 
X2 <- data$age15_18 
X3 <- data$age18_27 
X4 <- data$age27_45 
X5 <- data$age45_55 
X6 <- data$age55_65 
X7 <- data$age_o65 
X8 <- data$female 
X9 <- data$protestant 
X10 <- data$catholic 
X11 <- data$atheist_pop 
X12 <- data$foreign_pop 
X13 <- data$total_pop_ln 
X14 <- data$cdu11erst
dat <- data.frame(Y, Z, X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14)
ri_model3_emotional <- conduct_ri(formula = Y ~ Z + X0, X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + X13 + X14, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model3_emotional)[3]







## Model 4 Factual
Y <- data$cdu16zweit
Z <- data$treatment_rational
X0 <- data$treatment_emotional
X1 <- data$age6_15 
X2 <- data$age15_18 
X3 <- data$age18_27 
X4 <- data$age27_45 
X5 <- data$age45_55 
X6 <- data$age55_65 
X7 <- data$age_o65 
X8 <- data$female 
X9 <- data$protestant 
X10 <- data$catholic 
X11 <- data$atheist_pop 
X12 <- data$foreign_pop 
X13 <- data$total_pop_ln 
X14 <- data$cdu11zweit
dat <- data.frame(Y, Z, X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14)
ri_model4_factual <- conduct_ri(formula = Y ~ Z + X0, X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + X13 + X14, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model4_factual)[3]



## Model 4 Emotional
Y <- data$cdu16zweit
Z <- data$treatment_emotional
X0 <- data$treatment_rational
X1 <- data$age6_15 
X2 <- data$age15_18 
X3 <- data$age18_27 
X4 <- data$age27_45 
X5 <- data$age45_55 
X6 <- data$age55_65 
X7 <- data$age_o65 
X8 <- data$female 
X9 <- data$protestant 
X10 <- data$catholic 
X11 <- data$atheist_pop 
X12 <- data$foreign_pop 
X13 <- data$total_pop_ln 
X14 <- data$cdu11zweit
dat <- data.frame(Y, Z, X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14)
ri_model4_emotional <- conduct_ri(formula = Y ~ Z + X0, X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 + X9 + X10 + X11 + X12 + X13 + X14, declaration = declaration, assignment = "Z", sharp_hypothesis = 0, data = dat)
summary(ri_model4_emotional)[3]






